home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / TestTools / Sources / TestTool.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  11.3 KB  |  481 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TestTool.cp
  3.  
  4.     Contains:    MPW Tool to test out Classes.
  5.  
  6.     Description:
  7.         
  8.         TestTool [-v] [-x] [-s] [-l] [-n numReps] [-t] [-c ClassName]+ [-o …]
  9.         
  10.                -v       Turns verbose mode on (default is OFF)
  11.                -x       Turns debugging ON (default is OFF)
  12.                -p       Don't grow pool
  13.                -t       Trace facility ON (default is OFF
  14.                -s       Shutdown the ASLM
  15.                -l       Load the ASLM
  16.                -a      Do all tests
  17.                -n       <Count> Iteration count
  18.                -c       <YourTestTool> the name of a TTestTool subclass to instantiate
  19.                -o …    Remaining arguments are passed to <YourTestTool>::InitTest
  20.         
  21.         
  22.         This test program can be modified to test any tool you develop and do simple stress
  23.         testing by changing what is inside the test while loop.
  24.         
  25.     Caveats:
  26.     
  27.         Command-period will leave some things dangling that will never be disposed.
  28.         
  29.  
  30.     Copyright:    © 1991-1995 by Apple Computer, Inc., all rights reserved.
  31.  
  32. */
  33.  
  34. #ifndef __TYPES__
  35. #include <Types.h>
  36. #endif
  37. #ifndef __TESTTOOL__
  38. #include <TestTool.h>
  39. #endif
  40. #ifdef USEMPW
  41.     #ifndef __MPWSHAREDLIBS__
  42.     #include <MPWSharedLibs.h>
  43.     #endif
  44. #endif
  45. #ifndef __LIBRARYMANAGERCLASSES__
  46. #include <LibraryManagerClasses.h>
  47. #endif
  48. #ifndef __LIBRARYMANAGERUTILITIES__
  49. #include <LibraryManagerUtilities.h>
  50. #endif
  51.  
  52. #ifndef __QUICKDRAW__
  53. #include <QuickDraw.h>
  54. #endif
  55. #ifndef __EVENTS__
  56. #include <Events.h>
  57. #endif
  58. #ifndef __CTYPE__
  59. #include <ctype.h>
  60. #endif
  61. #ifndef __IOSTREAM__
  62. #include <iostream.h>
  63. #endif
  64. #ifndef __STDIO__
  65. #include <stdio.h>
  66. #endif
  67. #ifndef __CURSORCTL__
  68. #include <CursorCtl.h>
  69. #endif
  70.  
  71. static int    getIntArgument(char *arg);
  72. static void    Idle(RgnHandle);
  73. #ifndef USEMPW
  74. static int    _CDECL myPrintFunc(const char*, char*);
  75. #endif
  76.  
  77. const size_t    kTestPoolSize = 10000;
  78.  
  79. #include <time.h>
  80. #include <string.h>
  81. extern "C" void CleanupMPWLibs(void);
  82.  
  83. main(int argc, char *argv[]) 
  84. {
  85.  
  86.     int                idx;
  87.     int                argIdx;
  88.     int                theCount        = 1;
  89.     int                numClasses        = 0;
  90.     Boolean            verbose            = false;
  91.     Boolean            trace            = false;
  92.     Boolean            debugging        = false;
  93.     Boolean            nogrowpool        = false;
  94.     Boolean            doAll            = false;
  95.     Boolean            doLoad            = true;
  96.     TPoolNotifier*    savedNotifier = NULL;
  97.     RgnHandle        myRgn;
  98.     char*        classes[50];
  99.     
  100.     InitGraf(&qd.thePort);                // initialize quickdraw so we can use regions
  101.     myRgn = NewRgn();                    // a region to use for WaitNextEvent
  102.  
  103.     InitCursorCtl(NULL);
  104.  
  105. #ifdef USEMPW
  106.     InitLibraryManager(kTestPoolSize, kApplicZone);
  107.     TLibraryManager* testMgr = GetLocalLibraryManager();
  108.     
  109.     if (testMgr == NULL) 
  110.     {
  111.         cout << "### ERROR: Could not allocate a TLibraryManager" << endl;
  112.         return 1;
  113.     }
  114.     //
  115.     // Initialize MPW Library Callbacks
  116.     //
  117.     if (InitStdIOCallbacks() != 0)
  118.     {
  119.         cout << "### ERROR: Could not initialize StdIO callbacks" << endl;
  120.         return 1;
  121.     }
  122.     
  123.     ctime(0);
  124.     strerror(0);
  125.     
  126. #endif
  127.  
  128.     /*    -----------------------------------------------------------------
  129.         Parse the arguments
  130.         ----------------------------------------------------------------- */
  131.  
  132.     if (argc < 2)
  133.     {
  134.         fprintf(stderr, "# Usage - %s [-v] [-x] [-s] [-l] [-n numReps] [-t] [-c ClassName]+ [-o …]\n", argv[0]);
  135.         fprintf(stderr, "        -v       Verbose\n");
  136.         fprintf(stderr, "        -x       Debugging ON (default is OFF)\n");
  137.         fprintf(stderr, "        -p       Don't grow pool\n");
  138.         fprintf(stderr, "        -t       Trace facility ON (default is OFF\n");
  139.         fprintf(stderr, "        -s       Shutdown the ASLM\n");
  140.         fprintf(stderr, "        -nl    Don't do a Load on the classes\n");
  141.         fprintf(stderr, "        -l       Load the ASLM\n");
  142.         fprintf(stderr, "        -a    Do all tests\n");
  143.         fprintf(stderr, "        -n       <Count> Iteration count\n");
  144.         fprintf(stderr, "        -c       <YourTestTool> the name of a TTestTool subclass to instantiate\n");
  145.         fprintf(stderr, "        -o …  Remaining arguments are passed to <YourTestTool>::InitTest\n");
  146.         return 1;
  147.     }
  148.     
  149.     for (idx = 1; idx < argc && argv[idx][0] == '-' && argv[idx][1] != 'o'; idx++) 
  150.     {
  151.         if (argv[idx][0] == '-') 
  152.         { 
  153.             switch (tolower(argv[idx][1])) 
  154.             {
  155.                 case 'a':
  156.                     if (numClasses != 0)
  157.                     {
  158.                         fprintf(stderr, "### -a and -c are mutually exlusive switches!\n");
  159.                         return 1;
  160.                     }
  161.                     doAll = true;
  162.                     break;
  163.                     
  164.                 case 'n': 
  165.                     if (tolower(argv[idx][2]) == 'l')
  166.                     {
  167.                         doLoad = false;
  168.                     }
  169.                     else
  170.                     if (++idx < argc)
  171.                     {
  172.                         theCount = getIntArgument(argv[idx]);
  173.                         if (theCount < 0) 
  174.                         {
  175.                             fprintf(stderr, "### option -n value is not a positive integer: %s\n", 
  176.                                     argv[idx]);
  177.                             return 1;
  178.                         }
  179.                     }
  180.                     else 
  181.                     {
  182.                         fprintf(stderr, "### Not enough arguments\n");
  183.                         return 1;
  184.                     }
  185.                     break;
  186.                     
  187.                 case 'l':
  188.                     if (verbose)
  189.                         cout << "Loading the ASLM" << endl;
  190.                     if (!LoadLibraryManager())
  191.                     {
  192.                         cout << "### ERROR: the ASLM could not be loaded!\n";
  193.                         return 10;
  194.                     }
  195.                     return 0;
  196.                 
  197.                 case 'v':
  198.                     verbose = true;
  199.                     break;
  200.                 
  201.                 case 't':
  202.                     trace = true;
  203.                     break;
  204.                     
  205.                 case 's':
  206.                     if (verbose)
  207.                         cout << "Unloading the ASLM" << endl;
  208.                     UnloadLibraryManager();
  209.                     if (idx+1 < argc && tolower(*(argv[idx+1]+1)) == 'l')
  210.                         if (!LoadLibraryManager())
  211.                         {
  212.                             cout << "### ERROR: the ASLM could not be loaded!\n";
  213.                             return 10;
  214.                         }
  215.                     return 0;
  216.  
  217.                 case 'p':
  218.                     nogrowpool = true;
  219.                     break;
  220.                     
  221.                 case 'x':
  222.                     debugging = true;
  223.                     break;
  224.                     
  225.                 case 'c':
  226.                     if (doAll)
  227.                     {
  228.                         fprintf(stderr, "### -a and -c are mutually exlusive switches!\n");
  229.                         return 1;
  230.                     }
  231.                     idx++;
  232.                     classes[numClasses] = new char[strlen(kTestToolPrefix) +
  233.                                                    strlen(argv[idx]) + 1];
  234.                     strcpy(classes[numClasses], kTestToolPrefix);
  235.                     strcat(classes[numClasses++], argv[idx]);
  236.                     break;
  237.  
  238.                 default:
  239.                     fprintf(stderr, "# Usage - %s [-v] [-x] [-s] [-l] [-n numReps] [-t] [-c ClassName]+ [-o …]\n", argv[0]);
  240.                     fprintf(stderr, "        -v       Verbose\n");
  241.                     fprintf(stderr, "        -x       Debugging ON (default is OFF)\n");
  242.                     fprintf(stderr, "        -p       Don't grow pool\n");
  243.                     fprintf(stderr, "        -t       Trace facility ON (default is OFF\n");
  244.                     fprintf(stderr, "        -s       Shutdown the ASLM\n");
  245.                     fprintf(stderr, "        -l       Load the ASLM\n");
  246.                     fprintf(stderr, "        -a    Do all tests\n");
  247.                     fprintf(stderr, "        -n       <Count> Iteration count\n");
  248.                     fprintf(stderr, "        -c       <YourTestTool> the name of a TTestTool subclass to instantiate\n");
  249.                     fprintf(stderr, "        -o …  Remaining arguments are passed to <YourTestTool>::InitTest\n");
  250.                     return 1;
  251.                 
  252.             }
  253.         }
  254.     }
  255.     
  256.     if (argv[idx][0] == '-' && argv[idx][1] == 'o')
  257.         idx++;
  258.     
  259.     argc -= idx;
  260.     
  261.     argIdx = idx;
  262.     
  263. #ifndef USEMPW
  264.     if (debugging) 
  265.         DebugStr("\pAbout to call InitLibraryManager");
  266.  
  267.     InitLibraryManager(kTestPoolSize, kApplicZone);
  268.     TLibraryManager* testMgr = GetLocalLibraryManager();
  269.     
  270.     if (testMgr == NULL) 
  271.     {
  272.         cout << "### ERROR: Could not allocate a TLibraryManager" << endl;
  273.         return 1;
  274.     }
  275. #endif
  276.     
  277.     if (doAll)
  278.     {
  279.         OSErr    err;
  280.         
  281.         numClasses = 0;
  282.         
  283.         TClassInfo*    info = testMgr->GetClassInfo(ClassID(kTTestToolID), &err);
  284.         if (info == NULL)
  285.         {
  286.             cout << "### ERROR: Could not get a TClassInfo (err = )" << err << endl;
  287. #ifdef USEMPW
  288.             CleanupMPWLibraries();
  289. #endif
  290.             CleanupLibraryManager();
  291.             return 1;
  292.         }
  293.         char*    str;
  294.         while ((str = (char*)info->Next()) != NULL)
  295.         {
  296.             classes[numClasses] = new char[strlen(str) + 1];
  297.             strcpy(classes[numClasses++], str);
  298.         }
  299.         delete info;
  300.     }
  301.     
  302.     
  303.     TStandardPool* thePool = GetLocalPool();
  304.     
  305.     savedNotifier = thePool->GetNotifier();
  306.     if (nogrowpool)
  307.         thePool->SetNotifier(NULL);
  308.         
  309.     PoolInfo    info;
  310.     thePool->GetPoolInfo(info);
  311.     cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  312.     cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  313.     
  314.     // turn trace on or off according to -t option
  315.     if (!trace)
  316.         testMgr->TraceLogOff();
  317.     else
  318.         testMgr->TraceLogOn();
  319.     
  320.     // dump out TLibraryManager info
  321.     testMgr->Dump();
  322.     
  323.     for (idx = 0; idx < numClasses; ++idx)
  324.     {
  325.         TTestTool*    tool = NULL;
  326.         
  327.         Idle(myRgn);
  328.         
  329.         if (verbose)
  330.             cout << "INFO: Creating Tool " << classes[idx] << endl;
  331.             
  332.             
  333.         OSErr theErr;
  334.  
  335.         if (doLoad)
  336.         {
  337.             if (debugging)
  338.                 DebugStr("\pAbout to load class");
  339.             theErr = testMgr->LoadClass(ClassID(classes[idx]), true);
  340.             if (theErr != kNoError)
  341.             {
  342.                 fprintf(stderr, "###LoadClass failed, error = %d\n", theErr);
  343.                 continue;
  344.             }
  345.         }
  346.         else
  347.             if (debugging)
  348.                 DebugStr("\pAbout to new tool");
  349.         // LoadClass succeeded
  350.         tool = (TTestTool*)testMgr->NewObject(ClassID(classes[idx]), ClassID(kTTestToolID), &theErr);
  351.         if (theErr != kNoError)
  352.             fprintf(stderr, "###NewObject Error %d\n", theErr);
  353.         
  354.         if (tool == NULL)
  355.             cout << "### ERROR: Could not create Tool " << classes[idx] << endl;
  356.         else
  357.         {
  358.             tool->SetPool(thePool);
  359.             cout.flush();
  360.     #ifndef USEMPW
  361.             tool->SetPrintf(myPrintFunc);
  362.     #endif
  363.             
  364.             int count = theCount;
  365.             
  366.             if (verbose)
  367.             {
  368.                 PoolInfo    info;
  369.                 thePool->GetPoolInfo(info);
  370.                 cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  371.                 cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  372.             }
  373.             
  374.             tool->InitTest(verbose, debugging, argc, argv+argIdx);
  375.             Idle(myRgn);
  376.             while (count--)
  377.             {
  378.                 tool->RunTestIteration(verbose, debugging);
  379.                 Idle(myRgn);
  380.             }
  381.             tool->EndTest(verbose, debugging);
  382.             fflush(stdout);
  383.             Idle(myRgn);
  384.             
  385.             if (verbose)
  386.                 cout << "INFO: Destroying Tool " << classes[idx] << endl;
  387.             
  388.             if (debugging)
  389.                 DebugStr("\pAbout to destroy tool");
  390.     
  391.             delete tool;
  392.             if (verbose)
  393.             {
  394.                 PoolInfo    info;
  395.                 thePool->GetPoolInfo(info);
  396.                 cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  397.                 cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  398.                 if (nogrowpool)
  399.                 {
  400.                     void* test = thePool->Allocate(kTestPoolSize*2);
  401.                     if (test != NULL)
  402.                     {
  403.                         cout << "ERROR: Allocated block larger than pool size!" << endl;
  404.                         thePool->GetPoolInfo(info);
  405.                         cout << "ERROR: Pool size     = " << info.fFreeBytes << endl;
  406.                         cout << "ERROR: Largest Block = " << info.fLargestBlock << endl;
  407.                         thePool->Free(test);
  408.                         thePool->GetPoolInfo(info);
  409.                         cout << "ERROR: Pool size     = " << info.fFreeBytes << endl;
  410.                         cout << "ERROR: Largest Block = " << info.fLargestBlock << endl;
  411.                     }
  412.                     else
  413.                     {
  414.                         thePool->GetPoolInfo(info);
  415.                         cout << "INFO: After freeing chunks -->" << endl;
  416.                         cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  417.                         cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  418.                     }
  419.                 }
  420.             }
  421.             Idle(myRgn);
  422.         }
  423.         
  424.         if (doLoad)
  425.             testMgr->UnloadClass(ClassID(classes[idx]));
  426.     }
  427.  
  428.     if (verbose && numClasses > 1) 
  429.     {
  430.         PoolInfo    info;
  431.         thePool->GetPoolInfo(info);
  432.         cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  433.         cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  434.     }
  435.  
  436.     if (nogrowpool)
  437.         thePool->SetNotifier(savedNotifier);
  438.     
  439.     if (verbose) 
  440.         cout << "INFO: Disposing testMgr" << endl;
  441.     
  442. #ifdef USEMPW
  443.     CleanupMPWLibraries();
  444. #endif
  445.  
  446.     CleanupLibraryManager();        // delete the TLibraryManager and its pool
  447.     
  448.     DisposeRgn(myRgn);
  449.  
  450.     return 0;
  451. };
  452.  
  453. /**********************************************************************
  454. ** STATIC Functions
  455. ***********************************************************************/
  456.  
  457. #ifndef USEMPW
  458. static int myPrintFunc(const char* format, char* args)
  459. {
  460.     int    ret;
  461.     ret = vprintf(format, args);
  462.     fflush(stdout);
  463.     return ret;
  464. }
  465. #endif
  466.  
  467. static int getIntArgument(char *arg)
  468. {
  469.     int theCount = -1;
  470.     
  471.     sscanf(arg, "%d", &theCount);
  472.         
  473.     return theCount;
  474. }
  475.  
  476.  
  477. static void Idle(RgnHandle myRgn)
  478. {
  479.     EventRecord myEvent;
  480.     WaitNextEvent(nullEvent, &myEvent, 0, myRgn);
  481. }